package com.xk857.simpleeffect.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.fragment.LoginCodeFragment;
import com.xk857.simpleeffect.fragment.LoginFragment;
import com.xk857.simpleeffect.fragment.RegisterFragment;
import com.xuexiang.xui.utils.ViewUtils;

public class LoginActivity extends AppCompatActivity {

    private FrameLayout frameLayout;
    private TextView titleText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.main_bg));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_login);

        initView();
        showLogin();
    }

    private void initView() {
        frameLayout = findViewById(R.id.bottom_fragment);
        titleText = findViewById(R.id.textView);
    }

    // 显示登录框
    private void showLogin() {
        LoginFragment loginFragment = new LoginFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, loginFragment).commit();
        ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
        changeTitleText(R.string.login_in);

        RegisterFragment registerFragment = new RegisterFragment();
        LoginCodeFragment loginCodeFragment = new LoginCodeFragment();

        loginFragment.setOnClickListener(position -> {
            if (position == 0) {
                // 跳转注册页
                getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, registerFragment).commit();
                ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
                changeTitleText(R.string.sign_up);
            } else if (position == 1) {
                // 跳转到验证码登录页
                getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, loginCodeFragment).commit();
                ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
                changeTitleText(R.string.login_in);
            }
        });

        registerFragment.setOnClickListener(integer -> {
            getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, loginFragment).commit();
            ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
            changeTitleText(R.string.login_in);
        });

        // 注册成功，跳转到登录页
        registerFragment.setGoLogin(customer -> {
            loginFragment.setInitUsername(customer.getUsername());
            getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, loginFragment).commit();
            ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
            changeTitleText(R.string.login_in);
        });

        loginCodeFragment.setOnClickListener(position -> {
            if (position == 0) {
                // 跳转注册页
                getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, registerFragment).commit();
                ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
                changeTitleText(R.string.sign_up);
            } else if (position == 1) {
                // 跳转到密码登录页
                getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment, loginFragment).commit();
                ViewUtils.slideIn(frameLayout, 500, null, ViewUtils.Direction.BOTTOM_TO_TOP);
                changeTitleText(R.string.login_in);
            }
        });
    }


    /**
     * 更改文本框内容添加动画
     */
    private void changeTitleText(int sign_up) {
        titleText.setText(sign_up);
        ViewUtils.fadeIn(titleText, 500, null);
    }
}